package com.ruoyi.project.im.statistics.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.im.statistics.domain.Statistics;
import com.ruoyi.project.im.statistics.mapper.StatisticsMapper;
import com.ruoyi.project.im.statistics.service.StaticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class StatisticsServiceImpl implements StaticsService {
    @Autowired
    private StatisticsMapper statisticsMapper;


    @Override
    public Statistics getTodayByUserId(String userId) {
        return statisticsMapper.getTodayByUserId(userId);
    }


    @Override
    public Statistics getTodayActiveByUserId(String userId) {
        return statisticsMapper.getTodayActiveByUserId(userId);
    }


    @Override
    public List<Statistics> get30DayByUserId(String userId) {
        List<Statistics> list= statisticsMapper.get30DayByUserId(userId);
        List<Statistics>  day30=new ArrayList<>();
        for (int i = 0; i < 30; i++) {
           int index=0;
           Calendar calendar=Calendar.getInstance();
           calendar.add(Calendar.DAY_OF_MONTH,-i);
           String  strDate= DateUtils.dateTime(calendar.getTime());
           for (Statistics statistics : list) {
                String day=DateUtils.dateTime(statistics.getToday());
                if(strDate.equals(day)){
                    day30.add(statistics);
                    index=1;
                }
           }
           if(index==0){
               Statistics zero=new Statistics();
               zero.setToday(DateUtils.parseDate(strDate));
               zero.setNum(0);
               day30.add(zero);
           }
        }

        return day30;
    }

    @Override
    public Statistics getAllByUserId(String userId) {
        return statisticsMapper.getAllByUserId(userId);
    }
}
